package com.blb.edusearchservice.listen;

import com.alibaba.fastjson.JSON;
import com.blb.config.RabbitMQConfig;
import com.blb.edusearchservice.dao.ElasticsearchDao;
import com.blb.edusearchservice.service.ICourseIndexService;
import com.blb.pojo.Course;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 课程消息队列监听器
 */
@Slf4j
@Component
public class CourseMQListener {
    @Resource
    private ICourseIndexService  iCourseIndexService;

    @RabbitListener(queues = {RabbitMQConfig.QUEUE_COURSE_SAVE})
    public void handleSaveCourseMessage(String message){
        try {
            Course course= JSON.parseObject(message,Course.class);
            log.info("接收到保存课程消息：{}",message);
            iCourseIndexService.addOrUpdateCourse(course);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("保存发生错误");
        }

    }

    @RabbitListener(queues = {RabbitMQConfig.QUEUE_COURSE_REMOVE})
    public void handleRemoveCourseMessage(Long id){
        try {
            iCourseIndexService.deleteCourse(String.valueOf(id));
            log.info("接收到删除课程消息：{}",id);
        } catch (IOException e) {
            log.error("删除消息出错");
            e.printStackTrace();
        }

    }
}
